//
//  LHReadListController.m
//  EPA
//
//  Created by 卢浩 on 16/4/20.
//  Copyright © 2016年 卢浩. All rights reserved.
//

#import "LHReadListController.h"
#import "LHNetWorkRequestManager.h"
#import "LHReadListTableViewCell.h"
#import "LHReadListModel.h"
#import "LORefresh.h"
#import "LHBaseWebViewController.h"

@interface LHReadListController ()

@property (nonatomic,strong) NSMutableArray *readListData;

@property (nonatomic,strong) LORefresh *headerRefresh;

@property (nonatomic,strong) NSString *start;

@end

@implementation LHReadListController

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
//    self.tableView.contentInset = UIEdgeInsetsMake(150, 0, 0, 0);
}

- (void)dealloc
{
    //释放观察者
    
    //默认风火轮
//    [self.tableView removeObserver:[self.tableView defaultHeader] forKeyPath:@"contentOffset"];
    
    //自定义图片
    [self.tableView removeObserver:[self.tableView gifHeader] forKeyPath:@"contentOffset"];
    
    //底部
    [self.tableView removeObserver:[self.tableView defaultFooter] forKeyPath:@"contentOffset"];
    [self.tableView removeObserver:[self.tableView defaultFooter] forKeyPath:@"contentSize"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = self.typeName;
    
    self.readListData = [NSMutableArray array];
    
    self.start = @"0";
    
    [self loadDataWithTypeID:self.typeID Start:self.start];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"LHReadListTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"LHReadListTableViewCell"];
    
    //添加下拉刷新
    
    //默认风火轮
    __weak typeof(self) weakSelf = self;
//    [self.tableView addRefreshWithRefreshViewType:LORefreshViewTypeHeaderDefault refreshingBlock:^{
//        [weakSelf.readListData removeAllObjects];
//        [weakSelf.tableView reloadData];
//        weakSelf.start = @"0";
//        [weakSelf loadDataWithTypeID:weakSelf.typeID Start:weakSelf.start];
//    }];
    
    //自定义图片
    [self.tableView addRefreshWithRefreshViewType:LORefreshViewTypeHeaderGif refreshingBlock:^{
        [weakSelf.readListData removeAllObjects];
        [weakSelf.tableView reloadData];
        weakSelf.start = @"0";
        [weakSelf loadDataWithTypeID:weakSelf.typeID Start:weakSelf.start];
    }];
    self.headerRefresh = [self.tableView gifHeader];
    [self.headerRefresh setGifName:@"wangnima"];
    
    //添加上提加载更多
    [self.tableView addRefreshWithRefreshViewType:LORefreshViewTypeFooterDefault refreshingBlock:^{
        NSInteger count = weakSelf.readListData.count;
        weakSelf.start = [NSString stringWithFormat:@"%ld",(long)count];
        [weakSelf loadDataWithTypeID:weakSelf.typeID Start:weakSelf.start];
    }];
}

- (void)loadDataWithTypeID:(NSInteger)typeID Start:(NSString *)start{
    NSDictionary *paramsDict = @{@"deviceid":@"63A94D37-33F9-40FF-9EBB-481182338873",@"typeid":@(typeID),@"client":@"1",@"sort":@"addtime",@"limit":@(10),@"version":@"3.0.2",@"auth":[LHUserInfoManager shareManager].auth,@"start":start};
    [LHNetWorkRequestManager requestUrl:[NSURL URLWithString:READDETAILLIST_URL] Methed:POST Params:paramsDict Result:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (data) {
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
            NSDictionary *dataDict = dict[@"data"];
            NSArray *listArr = dataDict[@"list"];
            NSMutableArray *tempArr = [NSMutableArray array];
            for (NSDictionary *dict in listArr) {
                LHReadListModel *model = [[LHReadListModel alloc]init];
                [model setValuesForKeysWithDictionary:dict];
                [tempArr addObject:model];
//                NSLog(@"%@,%@,%@,%@",model.content,model.coverimg,model.name,model.title);
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                if (tempArr.count > 0) {
                    [self.readListData addObjectsFromArray:tempArr];
                    
                    [self.tableView reloadData];
                    
                    //刷新结束
                    //默认风火轮
                    //                [[self.tableView defaultHeader] endRefreshing];
                    //自定义图片
                    [[self.tableView gifHeader] endRefreshing];
                    //底部
                    [[self.tableView defaultFooter] endRefreshing];
                }else{
                    [self loadDataWithTypeID:self.typeID Start:self.start];
                }
            });
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.readListData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LHReadListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LHReadListTableViewCell" forIndexPath:indexPath];
    cell.model = self.readListData[indexPath.row];
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    LHBaseWebViewController *webVC = [[LHBaseWebViewController alloc]init];
    LHReadListModel *model = self.readListData[indexPath.row];
    webVC.contentid = model.ID;
    webVC.model = model;
    [self.navigationController pushViewController:webVC animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 143;
}

@end
